import _ from 'lodash';
import { propsComToViewCom } from '../';
import utils from '../../../../../utils';
import { unitWidthProp } from '../../../../../constants/form';

export const parseWidths = (widths, baseWidth) => {
  if (!widths.length) return [utils.toPixel(baseWidth)];

  return widths.map(width => {
    width = width || unitWidthProp;
    
    if (_.isNumber(width)) {
      width = parseFloat(width).toFixed(5) * baseWidth;
    }

    return utils.toPixel(width);
  });
}

export default {
  toViewData: (config) => {
    let { coms, defaultValue, defaultRowValue } = config;

    if (coms) {
      coms = coms.map(com => com && propsComToViewCom(com));
      defaultRowValue = coms.map(com => com && com.value);
      defaultValue = [_.cloneDeep(defaultRowValue)];
    }

    return {
      ...config,
      coms,
      defaultValue,
      defaultRowValue,
      errors: [[]],
    };
  }
}